package com.atguigu.guli.service.edu.api;


import com.atguigu.guli.service.base.helper.JwtHelper;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.vo.CourseCollectVo;
import com.atguigu.guli.service.edu.service.CourseCollectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2020-10-14
 */
@RestController
@RequestMapping("/api/edu/course-collect")
public class ApiCourseCollectController {
    @Autowired
    private CourseCollectService courseCollectService;

    @ApiOperation(value = "取消收藏课程")
    @DeleteMapping("auth/remove/{courseId}")
    public R removeCollect(@PathVariable String courseId,HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        Boolean isRemove = courseCollectService.removeCollectById(courseId,memberId);
        if (isRemove){
            return R.ok().message("取消成功");
        }
        return R.error().message("取消失败");
    }


    @ApiOperation(value = "获取课程收藏列表")
    @GetMapping("/auth/list")
    public R getCollectList(HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        List<CourseCollectVo> courseCollectVoList = courseCollectService.getCollectList(memberId);
        return R.ok().data("items",courseCollectVoList);
    }


    @ApiOperation(value = "收藏课程")
    @PostMapping("auth/save/{courseId}")
    public R saveCollect(@PathVariable String courseId,HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        Boolean saveCollect = courseCollectService.saveCollectById(courseId,memberId);
        if (saveCollect){
            return R.ok().message("收藏成功");
        }
        return R.error().message("收藏失败");
    }



    @ApiOperation(value = "判断是否收藏")
    @GetMapping("auth/is-collect/{courseId}")
    public R isCollect(@PathVariable String courseId, HttpServletRequest request){
        String memberId = JwtHelper.getId(request);
        Boolean isCollect = courseCollectService.getCollectById(courseId,memberId);
        if (isCollect){
            return R.ok().data("isCollect",true);
        }
        return R.error().data("isCollect", false);

    }

}

